package com.itqf.controller;

import com.itqf.entity.Product;
import com.itqf.enums.Rexum;
import com.itqf.service.ProductService;
import com.itqf.utils.R;
import com.itqf.vo.ProductVo;
import com.itqf.vo.ResultVo;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 作者:高少东
 * 项目名:  product_gsd
 * 时间: 2019/12/19 16:58
 * 描述:
 */


@Controller
public class ProductController {


    @Autowired
    ProductService productService;


    /**
     *  查看所有商品信息 ,并查询页面下拉框信息 ,枫叶查询  添加商品里的下拉框也用此接口
     * @param currentPage
     * @param pageSize
     * @return
     */

    @RequestMapping("selectAll")
    @ResponseBody
    public ProductVo selectAllProduct(@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "2") Integer pageSize){

       ProductVo  productVo = productService.selectAllProduct(currentPage,pageSize);

       return  productVo;
    }

    /**
     *  模糊查询商品信息
     */

    @PostMapping("query")
     @ResponseBody
     public ResultVo queryProduct(Product product){


          List<Product> products = productService.queryProduct(product);

          return R.ok(products);
     }


    /**
     *   添加商品
     */

     @PostMapping("addProduct")
     @ResponseBody
     public ResultVo addProduct(Product product){


         int  rows = productService.addProduct(product);

         if (rows>0){

//             return "forward:/productPage";
             return R.ok();
         }else {

//             return "productAddPage";
             return R.error(Rexum.NOT_KNOW_ERROR);
         }

      }


    /**
     * 上传图片
     */


    @PostMapping("shangchuang")
    @ResponseBody
     public ResultVo img(MultipartFile file) throws IOException {

         //前端传过来的参数名name
        String name = file.getName();
        System.out.println("name = " + name);

        String filename = file.getOriginalFilename();
        System.out.println("filename = " + filename);

        if (StringUtils.isEmpty(filename)){

            return  R.error(Rexum.NOT_KNOW_ERROR);
        }

        String path="D:\\Project(1910)\\product_gsd\\target\\classes\\static\\img";


        File file1 = new File(path);

        if (!file1.exists()){

            file1.mkdirs();
        }

        File file2 = new File(path,filename);


        file.transferTo(file2);



            return R.ok(filename);

    }


    /**
     * 删除商品
     */


    @RequestMapping("deleteProduct")
    public String deleteProduct(Product product){


        int  rows=  productService.deleteProduct(product);


        return "forward:/productPage";



    }


    /**
     * 修改商品
     */

    @PostMapping("modifyProduct")
    @ResponseBody
    public  ResultVo  modifyProduct(Product product){

        int rows= productService.modifyProduct(product);

        if (rows>0){

            return R.ok();
        }else{

            return R.error(Rexum.NOT_KNOW_ERROR);
        }
    }

}
